package sivan.test;

import java.util.concurrent.locks.ReentrantLock;

/**
 * 对象锁和类锁区别
 */
public class SynchronizedDemo {

    /**
     * @param name
     */
    public static synchronized void say(String name) {
        System.out.println(name + "说：==========");
        try {
            Thread.sleep(3000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static synchronized void eat(String name) {
        System.out.println(name + "吃:==========");
        try {
            Thread.sleep(3000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public synchronized void sayLove(String name) {
        System.out.println(name + "love:==========");
        try {
            Thread.sleep(3000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void sayLove1(String name) {
        synchronized (this) {
            System.out.println(name + "love1:==========");
            try {
                Thread.sleep(3000L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        SynchronizedDemo demo = new SynchronizedDemo();
        SynchronizedDemo demo1 = new SynchronizedDemo();
        Thread t1 = new Thread(() -> {
//            SynchronizedDemo.say("xiaowen");
            demo.sayLove1("xiaowen");
        });
        Thread t2 = new Thread(() -> {
//            SynchronizedDemo.eat("applle");
            demo.sayLove("xiaowen");
        });

        t1.start();
        t2.start();
    }

}
